/**
 * 
 */
package br.financeiro.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import br.financeiro.R;
import br.financeiro.entities.Mail;
import br.financeiro.enums.TypeMail;

/**
 * @author evaldosavio
 * 
 */
public class ItemEmailFormAdapter extends BaseAdapter {
	private List<Mail> mails;
	private LayoutInflater inflater;
	private AlertDialog.Builder dialog;
	private DialogInterface.OnClickListener l;
	private final Context context;

	public ItemEmailFormAdapter( Context context, List<Mail> mails ) {
		this.inflater = LayoutInflater.from( context );
		this.mails = mails;
		this.context = context;
	}

	private void createDialogTypes( final Mail mail ) {
		dialog = new AlertDialog.Builder( context );
		dialog.setTitle( "Selecione o tipo de email" );
		List<String> types = new ArrayList<String>();

		for ( TypeMail type : TypeMail.values() ) {
			types.add( type.toString() );
		}

		l = new DialogInterface.OnClickListener() {

			@Override
			public void onClick( DialogInterface dialog, int which ) {
				mail.setType( TypeMail.values()[which] );
				dialog.cancel();
				notifyDataSetChanged();
			}
		};

		dialog.setSingleChoiceItems( types.toArray( new String[types.size()] ), mail.getType().ordinal(), l );
	}

	@Override
	public int getCount() {
		return mails.size();
	}

	@Override
	public Object getItem( int id ) {
		return mails.get( id );
	}

	@Override
	public long getItemId( int id ) {
		return id;
	}

	@Override
	public View getView( int position, View view, ViewGroup groupView ) {
		if ( view == null ) {
			view = inflater.inflate( R.layout.form_mail, null );
		}
		// configure edit
		EditText edit = (EditText) view.findViewById( R.id.editPersonMail );
		edit.setText( mails.get( position ).getMail() );
		edit.setTag( mails.get( position ) );

		setEditTextWatcher( edit );

		ImageButton btn = (ImageButton) view.findViewById( R.id.type );
		btn.setImageResource( mails.get( position ).getType().getIcon() );

		setButtonAction( position, btn );
		return view;
	}

	private void setButtonAction( int position, ImageButton btn ) {
		btn.setTag( mails.get( position ) );
		btn.setOnClickListener( new OnClickListener() {

			@Override
			public void onClick( View v ) {
				Mail mail = (Mail) v.getTag();
				createDialogTypes( mail );
				dialog.show();
			}
		} );
	}

	// binde edit value in listEmail
	private void setEditTextWatcher( final EditText edit ) {
		edit.addTextChangedListener( new TextWatcher() {

			@Override
			public void onTextChanged( CharSequence s, int start, int before, int after ) {

			}

			@Override
			public void beforeTextChanged( CharSequence s, int start, int count, int after ) {}

			@Override
			public void afterTextChanged( Editable s ) {
				Mail mail = (Mail) edit.getTag();
				mail.setMail( s.toString() );
			}
		} );
	}
}
